home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / Multimedia / ^DivX_Article / virtualdub / VirtualDub-source-1_4d / oshelper.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-20  |  11.6 KB  |  507 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    Copyright (C) 1998-2001 Avery Lee
  3. //
  4. //    This program is free software; you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation; either version 2 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program; if not, write to the Free Software
  16. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include "VirtualDub.h"
  19. #include <stdlib.h>
  20.  
  21. #include <windows.h>
  22. #include <shellapi.h>
  23. #include "oshelper.h"
  24.  
  25. void Draw3DRect(HDC hDC, LONG x, LONG y, LONG dx, LONG dy, BOOL inverted) {
  26.     HPEN hPenOld;
  27.  
  28.     hPenOld = (HPEN)SelectObject(hDC, GetStockObject(inverted ? WHITE_PEN : BLACK_PEN));
  29.     MoveToEx(hDC, x, y+dy-1, NULL);
  30.     LineTo(hDC, x+dx-1, y+dy-1);
  31.     LineTo(hDC, x+dx-1, y);
  32.     DeleteObject(SelectObject(hDC, GetStockObject(inverted ? BLACK_PEN : WHITE_PEN)));
  33.     MoveToEx(hDC, x, y+dy-1, NULL);
  34.     LineTo(hDC, x, y);
  35.     LineTo(hDC, x+dx-1, y);
  36.     DeleteObject(SelectObject(hDC, hPenOld));
  37. }
  38.  
  39. // We follow MAME32's lead and put our keys in:
  40. //
  41. //    HKEY_CURRENT_USER\Software\Freeware\VirtualDub\
  42.  
  43. HKEY OpenConfigKey(const char *szKeyName) {
  44.     char temp[MAX_PATH]="Software\\Freeware\\VirtualDub";
  45.     HKEY hkey;
  46.  
  47.     if (szKeyName) {
  48.         strcat(temp, "\\");
  49.         strcat(temp, szKeyName);
  50.     }
  51.  
  52.     return RegOpenKeyEx(HKEY_CURRENT_USER, temp, 0, KEY_ALL_ACCESS, &hkey)==ERROR_SUCCESS
  53.             ? hkey
  54.             : NULL;
  55. }
  56.  
  57. HKEY CreateConfigKey(const char *szKeyName) {
  58.     char temp[MAX_PATH]="Software\\Freeware\\VirtualDub";
  59.     HKEY hkey;
  60.     DWORD dwDisposition;
  61.  
  62.     if (szKeyName) {
  63.         strcat(temp, "\\");
  64.         strcat(temp, szKeyName);
  65.     }
  66.  
  67.     return RegCreateKeyEx(HKEY_CURRENT_USER, temp, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwDisposition)==ERROR_SUCCESS
  68.             ? hkey
  69.             : NULL;
  70. }
  71.  
  72. BOOL DeleteConfigValue(const char *szKeyName, const char *szValueName) {
  73.     HKEY hkey;
  74.     BOOL success;
  75.  
  76.     if (!(hkey = OpenConfigKey(szKeyName)))
  77.         return FALSE;
  78.  
  79.     success = (RegDeleteValue(hkey, szValueName) == ERROR_SUCCESS);
  80.  
  81.     RegCloseKey(hkey);
  82.  
  83.     return success;
  84. }
  85.  
  86. BOOL QueryConfigString(const char *szKeyName, const char *szValueName, char *lpBuffer, int cbBuffer) {
  87.     HKEY hkey;
  88.     BOOL success;
  89.     DWORD type;
  90.  
  91.     if (!(hkey = OpenConfigKey(szKeyName)))
  92.         return FALSE;
  93.  
  94.     success = (ERROR_SUCCESS == RegQueryValueEx(hkey, szValueName, 0, &type, (LPBYTE)lpBuffer, (LPDWORD)&cbBuffer));
  95.  
  96.     RegCloseKey(hkey);
  97.  
  98.     return success;
  99. }
  100.  
  101. DWORD QueryConfigBinary(const char *szKeyName, const char *szValueName, char *lpBuffer, int cbBuffer) {
  102.     HKEY hkey;
  103.     BOOL success;
  104.     DWORD type;
  105.     DWORD size = cbBuffer;
  106.  
  107.     if (!(hkey = OpenConfigKey(szKeyName)))
  108.         return 0;
  109.  
  110.     success = (ERROR_SUCCESS == RegQueryValueEx(hkey, szValueName, 0, &type, (LPBYTE)lpBuffer, (LPDWORD)&size));
  111.  
  112.     RegCloseKey(hkey);
  113.  
  114.     return success ? size : 0;
  115. }
  116.  
  117. BOOL QueryConfigDword(const char *szKeyName, const char *szValueName, DWORD *lpdwData) {
  118.     HKEY hkey;
  119.     BOOL success;
  120.     DWORD type;
  121.     DWORD size = sizeof(DWORD);
  122.  
  123.     if (!(hkey = OpenConfigKey(szKeyName)))
  124.         return 0;
  125.  
  126.     success = (ERROR_SUCCESS == RegQueryValueEx(hkey, szValueName, 0, &type, (LPBYTE)lpdwData, (LPDWORD)&size));
  127.  
  128.     RegCloseKey(hkey);
  129.  
  130.     return success;
  131. }
  132.  
  133. BOOL SetConfigString(const char *szKeyName, const char *szValueName, const char *lpBuffer) {
  134.     HKEY hkey;
  135.     BOOL success;
  136.  
  137.     if (!(hkey = CreateConfigKey(szKeyName)))
  138.         return FALSE;
  139.  
  140.     success = (ERROR_SUCCESS == RegSetValueEx(hkey, szValueName, 0, REG_SZ, (LPBYTE)lpBuffer, strlen(lpBuffer)+1));
  141.  
  142.     RegCloseKey(hkey);
  143.  
  144.     return success;
  145. }
  146.  
  147. BOOL SetConfigBinary(const char *szKeyName, const char *szValueName, const char *lpBuffer, int cbBuffer) {
  148.     HKEY hkey;
  149.     BOOL success;
  150.  
  151.     if (!(hkey = CreateConfigKey(szKeyName)))
  152.         return FALSE;
  153.  
  154.     success = (ERROR_SUCCESS == RegSetValueEx(hkey, szValueName, 0, REG_BINARY, (LPBYTE)lpBuffer, cbBuffer));
  155.  
  156.     RegCloseKey(hkey);
  157.  
  158.     return success;
  159. }
  160.  
  161. BOOL SetConfigDword(const char *szKeyName, const char *szValueName, DWORD dwData) {
  162.     HKEY hkey;
  163.     BOOL success;
  164.  
  165.     if (!(hkey = CreateConfigKey(szKeyName)))
  166.         return FALSE;
  167.  
  168.     success = (ERROR_SUCCESS == RegSetValueEx(hkey, szValueName, 0, REG_DWORD, (LPBYTE)&dwData, sizeof(DWORD)));
  169.  
  170.     RegCloseKey(hkey);
  171.  
  172.     return success;
  173. }
  174.  
  175. ///////////////////////////////////////////////////////////////////////////
  176. //
  177. //    help support
  178. //
  179. ///////////////////////////////////////////////////////////////////////////
  180.  
  181. static char g_szHelpPath[MAX_PATH]="VirtualD.hlp";
  182.  
  183. void HelpSetPath() {
  184.     char szPath[MAX_PATH];
  185.     char *lpFilePart;
  186.     char *ext = NULL;
  187.  
  188.     if (GetModuleFileName(NULL, szPath, sizeof szPath))
  189.         if (GetFullPathName(szPath, sizeof g_szHelpPath, g_szHelpPath, &lpFilePart))
  190.             strcpy(lpFilePart,"VirtualD.hlp");
  191. }
  192.  
  193. const char *HelpGetPath() {
  194.     return g_szHelpPath;
  195. }
  196.  
  197. void HelpShowHelp(HWND hwnd) {
  198.     WinHelp(hwnd, g_szHelpPath, HELP_FINDER, 0);
  199. }
  200.  
  201. void HelpContext(HWND hwnd, DWORD helpID) {
  202.     WinHelp(hwnd, g_szHelpPath, HELP_CONTEXT, helpID);
  203. }
  204.  
  205. void HelpPopup(HWND hwnd, DWORD helpID) {
  206.     WinHelp(hwnd, g_szHelpPath, HELP_CONTEXTPOPUP, helpID);
  207. }
  208.  
  209. void HelpPopupByID(HWND hwnd, DWORD ctrlID, const DWORD *lookup) {
  210.     while(lookup[0]) {
  211.         if (lookup[0] == ctrlID)
  212.             HelpPopup(hwnd, lookup[1]);
  213.  
  214.         lookup+=2;
  215.     }
  216. }
  217.  
  218.  
  219. ///////////////////////////////////////////////////////////////////////////
  220. //
  221. //    disk free space
  222. //
  223. ///////////////////////////////////////////////////////////////////////////
  224.  
  225. static HINSTANCE g_hInstKernel32 = NULL;
  226. static BOOL (__stdcall *g_fpGetDiskFreeSpaceEx)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER) = NULL;
  227. static bool g_triedGetDiskFreeSpaceEx = false;
  228.  
  229. __int64 MyGetDiskFreeSpace(const char *lpszRoot) {
  230.     __int64 client_free, total_space, free_space;
  231.     DWORD dwSectorsPerCluster, dwBytesPerSector, dwFreeClusters, dwTotalClusters;
  232.     char tmp[MAX_PATH];
  233.  
  234.     if (!g_hInstKernel32 && !(g_hInstKernel32 = LoadLibrary("kernel32.dll")))
  235.         return -1;
  236.  
  237.     if (!g_triedGetDiskFreeSpaceEx) {
  238.         g_fpGetDiskFreeSpaceEx = (BOOL (__stdcall *)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER))GetProcAddress(g_hInstKernel32, "GetDiskFreeSpaceExA");
  239.         g_triedGetDiskFreeSpaceEx = true;
  240.     }
  241.  
  242.     lpszRoot = SplitPathRoot(tmp, lpszRoot);
  243.  
  244.     if (g_fpGetDiskFreeSpaceEx) {
  245.         return g_fpGetDiskFreeSpaceEx(lpszRoot, (PULARGE_INTEGER)&client_free, (PULARGE_INTEGER)&total_space, (PULARGE_INTEGER)&free_space)
  246.             ? free_space
  247.             : -1;
  248.     } else {
  249.         return GetDiskFreeSpace(lpszRoot, &dwSectorsPerCluster, &dwBytesPerSector, &dwFreeClusters, &dwTotalClusters)
  250.             ? dwFreeClusters * dwSectorsPerCluster * dwBytesPerSector
  251.             : -1;
  252.     }
  253. }
  254.  
  255.  
  256. const char *SplitPathName(const char *path) {
  257.     const char *s = path;
  258.  
  259.     while(*s) ++s;
  260.  
  261.     while(s>path && s[-1]!='\\' && s[-1]!=':')
  262.         --s;
  263.  
  264.     return s;
  265. }
  266.  
  267. const char *SplitPathExt(const char *path) {
  268.     const char *s = path;
  269.     const char *tail;
  270.  
  271.     while(*s) ++s;
  272.  
  273.     tail = s;
  274.  
  275.     while(s>path && s[-1]!='\\' && s[-1]!=':') {
  276.         if (s[-1]=='.')
  277.             return s-1;
  278.  
  279.         --s;
  280.     }
  281.  
  282.     return tail;
  283. }
  284.  
  285. char *MergePath(char *path, const char *fn) {
  286.     char *slash=NULL, *colon=NULL;
  287.     char *s = path;
  288.  
  289.     if (!*s) {
  290.         strcpy(path, fn);
  291.         return path;
  292.     }
  293.  
  294.     while(*s)
  295.         ++s;
  296.  
  297.     if (s[-1]!='\\' && s[-1]!=':')
  298.         *s++ = '\\';
  299.  
  300.     strcpy(s, fn);
  301.  
  302.     return path;
  303. }
  304.  
  305. char *SplitPathRoot(char *dst, const char *path) {
  306.  
  307.     if (!path)
  308.         return NULL;
  309.  
  310.     // C:
  311.  
  312.     if (isalpha(path[0]) && path[1]==':') {
  313.         dst[0] = path[0];
  314.         dst[1] = ':';
  315.         dst[2] = '\\';
  316.         dst[3] = 0;
  317.  
  318.         return dst;
  319.     }
  320.  
  321.     // UNC path?
  322.  
  323.     if (path[0] == '\\' && path[1] == '\\') {
  324.         const char *s = path+2;
  325.         char *t = dst;
  326.  
  327.         *t++ = '\\';
  328.         *t++ = '\\';
  329.  
  330.         while(*s && *s != '\\')
  331.             *t++ = *s++;
  332.  
  333.         if (*s)
  334.             *t++ = *s++;
  335.  
  336.         while(*s && *s != '\\')
  337.             *t++ = *s++;
  338.  
  339.         *t++ = '\\';
  340.         *t = 0;
  341.  
  342.         return dst;
  343.     }
  344.  
  345.     return NULL;
  346. }
  347.  
  348. bool IsFilenameOnFATVolume(const char *pszFilename) {
  349.     char szFileRoot[MAX_PATH];
  350.     DWORD dwMaxComponentLength;
  351.     DWORD dwFSFlags;
  352.     char szFilesystem[MAX_PATH];
  353.  
  354.     if (!GetVolumeInformation(SplitPathRoot(szFileRoot, pszFilename),
  355.             NULL, 0,        // Volume name buffer
  356.             NULL,            // Serial number buffer
  357.             &dwMaxComponentLength,
  358.             &dwFSFlags,
  359.             szFilesystem,
  360.             sizeof szFilesystem))
  361.         return false;
  362.  
  363.     return !strnicmp(szFilesystem, "FAT", 3);
  364. }
  365.  
  366. ///////////////////////////////////////////////////////////////////////////
  367.  
  368. void LaunchURL(const char *pURL) {
  369.     ShellExecute(NULL, "open", pURL, NULL, NULL, SW_SHOWNORMAL);
  370. }
  371.  
  372. ///////////////////////////////////////////////////////////////////////////
  373.  
  374. bool EnableCPUTracking() {
  375.     HKEY hOpen;
  376.     DWORD cbData;
  377.     DWORD dwType;
  378.     LPBYTE pByte;
  379.     DWORD rc;
  380.  
  381.     bool fSuccess = true;
  382.  
  383.     if ( (rc = RegOpenKeyEx(HKEY_DYN_DATA,"PerfStats\\StartStat", 0,
  384.                     KEY_READ, &hOpen)) == ERROR_SUCCESS) {
  385.  
  386.         // query to get data size
  387.         if ( (rc = RegQueryValueEx(hOpen,"KERNEL\\CPUUsage",NULL,&dwType,
  388.                 NULL, &cbData )) == ERROR_SUCCESS) {
  389.  
  390.             pByte = (LPBYTE)allocmem(cbData);
  391.  
  392.             rc = RegQueryValueEx(hOpen,"KERNEL\\CPUUsage",NULL,&dwType, pByte,
  393.                               &cbData );
  394.  
  395.             freemem(pByte);
  396.         } else
  397.             fSuccess = false;
  398.  
  399.         RegCloseKey(hOpen);
  400.     } else
  401.         fSuccess = false;
  402.  
  403.     return fSuccess;
  404. }
  405.  
  406. bool DisableCPUTracking() {
  407.     HKEY hOpen;
  408.     DWORD cbData;
  409.     DWORD dwType;
  410.     LPBYTE pByte;
  411.     DWORD rc;
  412.  
  413.     bool fSuccess = true;
  414.  
  415.     if ( (rc = RegOpenKeyEx(HKEY_DYN_DATA,"PerfStats\\StopStat", 0,
  416.                     KEY_READ, &hOpen)) == ERROR_SUCCESS) {
  417.  
  418.         // query to get data size
  419.         if ( (rc = RegQueryValueEx(hOpen,"KERNEL\\CPUUsage",NULL,&dwType,
  420.                 NULL, &cbData )) == ERROR_SUCCESS) {
  421.  
  422.             pByte = (LPBYTE)allocmem(cbData);
  423.  
  424.             rc = RegQueryValueEx(hOpen,"KERNEL\\CPUUsage",NULL,&dwType, pByte,
  425.                               &cbData );
  426.  
  427.             freemem(pByte);
  428.         } else
  429.             fSuccess = false;
  430.  
  431.         RegCloseKey(hOpen);
  432.     } else
  433.         fSuccess = false;
  434.  
  435.     return fSuccess;
  436. }
  437.  
  438. CPUUsageReader::CPUUsageReader() {
  439.     FILETIME ftCreate, ftExit;
  440.  
  441.     hkeyKernelCPU = NULL;
  442.     fNTMethod = false;
  443.  
  444.     if (GetProcessTimes(GetCurrentProcess(), &ftCreate, &ftExit, (FILETIME *)&kt_last, (FILETIME *)&ut_last)) {
  445.  
  446.         // Using Windows NT/2000 method
  447.         GetSystemTimeAsFileTime((FILETIME *)&st_last);
  448.  
  449.         fNTMethod = true;
  450.  
  451.     } else {
  452.  
  453.         // Using Windows 95/98 method
  454.  
  455.         HKEY hkey;
  456.  
  457.         if (EnableCPUTracking()) {
  458.  
  459.             if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_DYN_DATA, "PerfStats\\StatData", 0, KEY_READ, &hkey)) {
  460.                 hkeyKernelCPU = hkey;
  461.             } else
  462.                 DisableCPUTracking();
  463.         }
  464.     }
  465. }
  466.  
  467. CPUUsageReader::~CPUUsageReader() {
  468.     if (hkeyKernelCPU) {
  469.         RegCloseKey(hkeyKernelCPU);
  470.         DisableCPUTracking();
  471.     }
  472. }
  473.  
  474. int CPUUsageReader::read() {
  475.  
  476.     if (hkeyKernelCPU) {
  477.         DWORD type;
  478.         DWORD dwUsage;
  479.         DWORD size = sizeof dwUsage;
  480.  
  481.         if (ERROR_SUCCESS == RegQueryValueEx(hkeyKernelCPU, "KERNEL\\CPUUsage", 0, &type, (LPBYTE)&dwUsage, (LPDWORD)&size))
  482.             return (int)dwUsage;
  483.         
  484.         return -1;
  485.     } else if (fNTMethod) {
  486.         FILETIME ftCreate, ftExit;
  487.         unsigned __int64 kt, st, ut;
  488.         int cpu;
  489.  
  490.         GetProcessTimes(GetCurrentProcess(), &ftCreate, &ftExit, (FILETIME *)&kt, (FILETIME *)&ut);
  491.         GetSystemTimeAsFileTime((FILETIME *)&st);
  492.  
  493.         if (st == st_last)
  494.             return 100;
  495.         else
  496.             cpu = (int)((100 * (kt + ut - kt_last - ut_last) + (st - st_last)/2) / (st - st_last));
  497.  
  498.         kt_last = kt;
  499.         ut_last = ut;
  500.         st_last = st;
  501.  
  502.         return cpu;
  503.     }
  504.  
  505.     return -1;
  506. }
  507.